﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Microsoft.Practices.Unity;
using Sispe.Entities;
using Sispe.BussinesLogic;

namespace Sispe.UI
{
    public partial class FrmAreaLiquidacion : Form
    {
        [Dependency]
        public IAreaLiquidacionBL areaBL { get; set; }

        [Dependency]
        public IUsuarioMenuBL usuMenuBL { get; set; }

        public FrmAreaLiquidacion()
        {
            InitializeComponent();
            this.BuildUp();
        }

        public USUARIO currentUsuarioAreaLiquidacion { get; set; }

        private void FrmMttoArea_Load(object sender, EventArgs e)
        {
            if (currentUsuarioAreaLiquidacion != null)
            {
                menuLlena();
            }
            else
                CargarDatos();
        }

        public void menuLlena()
        {
            var datoUsuario = usuMenuBL.GetAllFromUsuarioByPersonaFromLogin(currentUsuarioAreaLiquidacion.TRABAJADOR.PERSONA.Dni);

            var permiso = (from p in usuMenuBL.GetAllUsuarioMenu(datoUsuario.IdUsuario)
                           select p.MENU.Tag.ToString()).ToArray();

            System.Windows.Forms.Button[] botones = new System.Windows.Forms.Button[]{
            this.btnEliminar,this.btnEditar,this.btnNuevo};

            var noVisible = from nv in botones
                            where !permiso.Contains(nv.Tag.ToString())
                            select nv;

            noVisible.ToList().ForEach(nv => nv.Enabled = false);

            CargarDatos();
        }

        private void CargarDatos()
        {
            aREALIQUIDACIONBindingSource.DataSource = areaBL.GetAllFromArea();
        }

        private void btnBuscar_Click(object sender, EventArgs e)
        {
            aREALIQUIDACIONBindingSource.DataSource = areaBL.GetFromAreaByDescripcion(txtDescripcion.Text);
        }

        private void btnNuevo_Click(object sender, EventArgs e)
        {
            FrmMttoAreaLiquidacion mtto = new FrmMttoAreaLiquidacion();
            mtto.Nuevo();
            mtto.ShowDialog();
            CargarDatos();
        }

        private void btnEditar_Click(object sender, EventArgs e)
        {
            var currentArea = aREALIQUIDACIONBindingSource.Current as AREA_LIQUIDACION;

            if (currentArea != null)
            {

                FrmMttoAreaLiquidacion mtto = new FrmMttoAreaLiquidacion();
                mtto.currentArea = currentArea;
                mtto.Editar();
                mtto.ShowDialog();
                CargarDatos();

            }
            else
            {
                MessageBox.Show("Debe seleccionar un Área", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void btnEliminar_Click(object sender, EventArgs e)
        {
            AREA_LIQUIDACION currentreArea = aREALIQUIDACIONBindingSource.Current as AREA_LIQUIDACION;

            if (currentreArea != null)
            {

                var rpt = MessageBox.Show(this,
                  string.Format("¿Desea Eliminar el Área: {0}?", currentreArea.Descripcion),
                  "Mensaje",
                  MessageBoxButtons.YesNo,
                  MessageBoxIcon.Question);

                if (rpt == DialogResult.Yes)
                {
                    areaBL.RemoveArea(currentreArea.IdAreaLiquidacion);
                    CargarDatos();
                }

            }
            else
            {
                MessageBox.Show("Debe Seleccionar una Área", "Mensaje", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void txtDescripcion_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                aREALIQUIDACIONBindingSource.DataSource = areaBL.GetFromAreaByDescripcion(txtDescripcion.Text);
            }
        }

        private void txtDescripcion_KeyPress(object sender, KeyPressEventArgs e)
        {
            var coma = Convert.ToString(",");
            var punto = Convert.ToString(".");
            var parentesis = Convert.ToString("()");
            var puntoComa = Convert.ToString(";");
            var slash = Convert.ToString("/");

            string cadena = coma + punto + parentesis + puntoComa + slash + (char)8;

            if (Char.IsLetter(e.KeyChar) || cadena.Contains(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsControl(e.KeyChar))
            {
                e.Handled = false;
            }
            else if (Char.IsSeparator(e.KeyChar))
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }
        }
        bool isExtend;
        public void Extend()
        {
            isExtend = true;
        }
        
        public AREA_LIQUIDACION CurrentArea { get; set; }

        private void aREARadGridView_DoubleClick(object sender, EventArgs e)
        {
            if (isExtend == true)
            {
            CurrentArea = aREALIQUIDACIONBindingSource.Current as AREA_LIQUIDACION;
            Close();
            }
        }
    }
}
